// game.js for Perlenspiel 3.0.8
// Modifications by Mark Diehr: mdiehr@fullsail.com 
// Perlenspiel is a scheme by Professor Moriarty (bmoriarty@wpi.edu).
// Perlenspiel is Copyright © 2009-13 Worcester Polytechnic Institute.
// This file is part of Perlenspiel. 

// Please fill out these comments with the appropriate information.
//     Project Title:	
//            Author:	
//              Date:	

/* Project notes:
    You can include project notes, messages, instructions, and TODOs here if you wish.
    
*/

// Put your global variables after this line

var player = {
color : PS.COLOR_RED
}
var isHolding = false;
var isHoldingColor;
var curLevel = 1;
// Put your function definitions after this line
function winner()
{
	if(PS.data(9, 0) == "Red" && PS.color(8,0) == PS.COLOR_GREEN)
	{
		PS.statusText("Winner");
		curLevel++;
	}
}
function DrawLevel1()
{
	PS.border(PS.ALL, PS.ALL, 0);
	PS.border(9,0,2);
	PS.border(9,1,2);
	PS.border(9,2,2);
	PS.border(8,2,2);
	
	PS.borderColor(9,0,PS.COLOR_RED);
	PS.borderColor(9,1,PS.COLOR_RED);
	PS.borderColor(9,2,PS.COLOR_RED);
	PS.borderColor(8,2,PS.COLOR_RED);
	
	
	PS.border(8,0,2);
	PS.border(8,1,2);
	PS.border(7,0,2);
	PS.border(7,1,2);
	
	PS.borderColor(8,0,PS.COLOR_GREEN);
	PS.borderColor(8,1,PS.COLOR_GREEN);
	PS.borderColor(7,0,PS.COLOR_GREEN);
	PS.borderColor(7,1,PS.COLOR_GREEN);
	
	PS.color(9,3, PS.COLOR_BLACK);
}

function LevelChanger(levelnumber)
{
	if(1)
	{
		DrawLevel1();	
	}
	/*if(2)
	{
		DrawLevel2();
	}*/
}
// PS.init( system, options )
// Initializes the game
PS.init = function (system, options) {
	"use strict";

	// Use PS.gridSize( x, y ) to set the grid to
	// the initial dimensions you want (32 x 32 maximum)
	// Do this FIRST to avoid problems!
	// Otherwise you will get the default 8x8 grid

	PS.gridSize(10,10); // replace with your own x/y values

	// Add any other initialization code you need here
	
	LevelChanger(curLevel);
	

	PS.color( 4, 4, player.color);
	PS.color( 4, 5, player.color);
	PS.color( 4, 6, player.color);
	PS.color( 3, 6, player.color);
	
	
	PS.color( 0, 0, PS.COLOR_GREEN);
	PS.color( 1, 0, PS.COLOR_GREEN);
	PS.color( 0, 1, PS.COLOR_GREEN);
	PS.color( 1, 1, PS.COLOR_GREEN);
	

};

// PS.touch ( x, y, data, options )
// Called when the mouse button is clicked on a bead, or when a bead is touched
PS.touch = function (x, y, data, options) {
	"use strict";

	// Uncomment the following line to inspect parameters
	// PS.debug( "PS.touch() @ " + x + ", " + y + "\n" );

	// Add code here for mouse clicks/touches over a bead
	if(isHolding == false)
	{
		isHolding = true;
		isHoldingColor=PS.color(x, y);
	}

};

// PS.release ( x, y, data, options )
// Called when the mouse button is released over a bead, or when a touch is lifted off a bead
PS.release = function (x, y, data, options) {
	"use strict";

	// Uncomment the following line to inspect parameters
	// PS.debug( "PS.release() @ " + x + ", " + y + "\n" );

	// Add code here for when the mouse button/touch is released over a bead
	if(isHolding == true)
	{
		isHolding = false;
		isHoldingColor = null;
	}
	
};

// PS.enter ( x, y, button, data, options )
// Called when the mouse/touch enters a bead
PS.enter = function (x, y, data, options) {
	"use strict";

	// Uncomment the following line to inspect parameters
	// PS.debug( "PS.enter() @ " + x + ", " + y + "\n" );

	// Add code here for when the mouse cursor/touch enters a bead
	if(isHolding == true)
	{
		if(isHoldingColor == PS.COLOR_RED)
		{
		PS.color(x,y,isHoldingColor);
		PS.color(x,y+1,isHoldingColor);
		PS.color(x,y+2,isHoldingColor);
		PS.color(x-1,y+2,isHoldingColor);
		
		PS.data(x,y,"Red");
		PS.data(x,y+1,"Red");
		PS.data(x,y+2,"Red");
		PS.data(x-1,y+2,"Red");
		
		
		winner();
		}
		
		if(isHoldingColor == PS.COLOR_GREEN)
		{
		PS.color( x, y, PS.COLOR_GREEN);
		PS.color( x+1, y, PS.COLOR_GREEN);
		PS.color( x, y+1, PS.COLOR_GREEN);		
		PS.color( x+1, y+1, PS.COLOR_GREEN);
		winner();
		}
	}
};

// PS.exit ( x, y, data, options )
// Called when the mouse cursor/touch exits a bead
PS.exit = function (x, y, data, options) {
	"use strict";

	// Uncomment the following line to inspect parameters
	// PS.debug( "PS.exit() @ " + x + ", " + y + "\n" );

	// Add code here for when the mouse cursor/touch exits a bead
	if(isHolding == true)
	{
		
		if(isHoldingColor == PS.COLOR_RED)
		{
		PS.color(x,y,PS.COLOR_WHITE);
		PS.color(x,y+1,PS.COLOR_WHITE);
		PS.color(x,y+2,PS.COLOR_WHITE);
		PS.color(x-1,y+2,PS.COLOR_WHITE);
		
		PS.data(x,y,"");
		PS.data(x,y+1,"");
		PS.data(x,y+2,"");
		PS.data(x-1,y+2,"");
		}
		
		
		if(isHoldingColor == PS.COLOR_GREEN)
		{
		PS.color( x, y, PS.COLOR_WHITE);
		PS.color( x+1, y, PS.COLOR_WHITE);
		PS.color( x, y+1, PS.COLOR_WHITE);		
		PS.color( x+1, y+1, PS.COLOR_WHITE);
		}
		
	}
};

// PS.exitGrid ( options )
// Called when the mouse cursor/touch exits the grid perimeter
PS.exitGrid = function (options) {
	"use strict";

	// Uncomment the following line to verify operation
	// PS.debug( "PS.exitGrid() called\n" );

	// Add code here for when the mouse cursor/touch moves off the grid
};

// PS.keyDown ( key, shift, ctrl, options )
// Called when a key on the keyboard is pressed
PS.keyDown = function (key, shift, ctrl, options) {
	"use strict";

	// Uncomment the following line to inspect parameters
	// PS.debug( "PS.keyDown(): key = " + key + ", shift = " + shift + ", ctrl = " + ctrl + "\n" );

	// Add code here for when a key is pressed

};

// PS.keyUp ( key, shift, ctrl, options )
// Called when a key on the keyboard is released
PS.keyUp = function (key, shift, ctrl, options) {
	"use strict";

	// Uncomment the following line to inspect parameters
	// PS.debug( "PS.keyUp(): key = " + key + ", shift = " + shift + ", ctrl = " + ctrl + "\n" );

	// Add code here for when a key is released
};

// PS.input ( sensors, options )
// Called when an input device event (other than mouse/touch/keyboard) is detected
PS.input = function (sensors, options) {
	"use strict";

	// Uncomment the following block to inspect parameters
	/*
	PS.debug( "PS.input() called\n" );
	var device = sensors.wheel; // check for scroll wheel
	if ( device )
	{
	    PS.debug( "sensors.wheel = " + device + "\n" );
	}
	*/

	// Add code here for when an input event is detected
};